#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <iostream>
#include <unordered_map>
#include <queue>

using namespace std;

vector<int> findOrder(int n, vector<vector<int>>& p) {
    if (p.size() == 0)
    {
        vector<int> ret;
        for (int i = 0; i < n; ++i)
            ret.push_back(i);

        return ret;
    }

    unordered_map<int, vector<int>> edges;
    vector<int> in(n);

    for (auto& v : p)
    {
        int a = v[0], b = v[1];
        edges[b].push_back(a);
        in[a]++;
    }

    queue<int> q;
    vector<int> ret;

    for (int i = 0; i < n; ++i)
        if (in[i] == 0)
            q.push(i);

    while (q.size())
    {
        int b = q.front();q.pop();

        ret.push_back(b);

        for (auto& a : edges[b])
        {
            --in[a];
            if (in[a] == 0)
            {
                q.push(a);
            }
        }
    }

    for (int i = 0; i < n; ++i)
        if (in[i])
            return vector<int>();

    return ret;
}

int main()
{
    vector<vector<int>> arr;
    arr.emplace_back(1, 0);
    arr.emplace_back(1, 2);
    arr.emplace_back(0, 1);

    
    findOrder(3 , arr);
	return 0;
}